﻿using System.Collections.Generic;
using System.ComponentModel;
using SQLServerHealthCheck.Model;
using System.Linq;
using System.Windows.Media;
using System.Collections;
namespace PlayGround.WPF
{


    public class IssueViewModel : INotifyPropertyChanged
    {
        #region Data

        bool? _isChecked = false;
        IssueViewModel _parent;

        #endregion // Data

        #region Issues

        public static List<IssueViewModel> CreateIssues()
        {
            HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"].ToString());

            var servers = from s in dc.Servers
                          select s;

            IssueViewModel root = new IssueViewModel("All", new SolidColorBrush(Colors.Blue));
            root.IsInitiallySelected = true;

            foreach (var serve in servers)
            {


                IssueViewModel servernode = new IssueViewModel("Server: " + serve.Name, new SolidColorBrush(Colors.Blue));



                var services = from i in serve.Services
                               where i.ServiceTypeID == 1
                               select i;
                var serverissues = from si in dc.Issues
                                   where (si.IssueType.Scope.ScopeId == 1)
                                   && (si.TargetId == serve.ServerID)
                               && (si.Fixed != true || si.Fixed == null)
                                   && (si.IssueType.Fixable == true)
                                   select si;
                foreach (var si in serverissues)
                {


                    IssueViewModel serverissuenode = new IssueViewModel(si.Description, new SolidColorBrush(Colors.Green), si);
                    servernode.Children.Add(serverissuenode);
                }
                foreach (var servi in services)
                {


                    IssueViewModel servicenode = new IssueViewModel("Service: " + servi.InstanceName, new SolidColorBrush(Colors.Blue));

                    var databases = from i in servi.Databases
                                    select i;

                    var serviceissues = from si in dc.Issues
                                        where (si.IssueType.Scope.ScopeId == 2)
                                        && (si.TargetId == servi.ServiceID)
                                      && (si.Fixed != true || si.Fixed == null)
                                   && (si.IssueType.Fixable == true)
                                        select si;
                    foreach (var si in serviceissues)
                    {


                        IssueViewModel serviceissuenode = new IssueViewModel(si.Description, new SolidColorBrush(Colors.Green), si);
                        servicenode.Children.Add(serviceissuenode);
                    } foreach (var db in databases)
                    {


                        IssueViewModel databasenode = new IssueViewModel("Database: " + db.DatabaseName, new SolidColorBrush(Colors.Blue));
                        var databaseissues = from dbi in dc.Issues
                                             where (dbi.IssueType.Scope.ScopeId == 3)
                                             &&
                                             (dbi.TargetId == db.DatabaseID)
                                           && (dbi.Fixed != true || dbi.Fixed == null)
                                   && (dbi.IssueType.Fixable == true)
                                             select dbi;
                        foreach (var dbi in databaseissues)
                        {
                            IssueViewModel databaseissuenode = new IssueViewModel(dbi.Description, new SolidColorBrush(Colors.Green), dbi);
                            databasenode.Children.Add(databaseissuenode);
                        }

                        if (databasenode.Children.Count() != 0)
                            servicenode.Children.Add(databasenode);

                    }
                    if (servicenode.Children.Count() != 0)
                        servernode.Children.Add(servicenode);

                }
                if (servernode.Children.Count() != 0)
                    root.Children.Add(servernode);
            }



            root.Initialize();
            return new List<IssueViewModel> { root };

        }

        IssueViewModel(string text, SolidColorBrush kleur)
        {
            // this.issue = issue;
            this.Name = text;

            this.Children = new List<IssueViewModel>();
            this.kleur = kleur;
        }
        IssueViewModel(string text, SolidColorBrush kleur, Issue issue)
        {
            this.Issue = issue;
            this.Name = text;

            this.Children = new List<IssueViewModel>();
            this.kleur = kleur;
        }

        void Initialize()
        {
            foreach (IssueViewModel child in this.Children)
            {
                child._parent = this;
                child.Initialize();
            }
        }

        #endregion // CreateFoos

        #region Properties

        public List<IssueViewModel> Children { get; private set; }

        public bool IsInitiallySelected { get; private set; }

        public string Name { get; private set; }
        public Issue Issue { get; private set; }

        public SolidColorBrush kleur { get;  private set; }

        #region IsChecked

        /// <summary>
        /// Gets/sets the state of the associated UI toggle (ex. CheckBox).
        /// The return value is calculated based on the check state of all
        /// child FooViewModels.  Setting this property to true or false
        /// will set all children to the same check state, and setting it 
        /// to any value will cause the parent to verify its check state.
        /// </summary>
        public bool? IsChecked
        {
            get { return _isChecked; }
            set { this.SetIsChecked(value, true, true); }
        }

        void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
        {
            if (value == _isChecked)
                return;

            _isChecked = value;

            if (updateChildren && _isChecked.HasValue)
                this.Children.ForEach(c => c.SetIsChecked(_isChecked, true, false));

            if (updateParent && _parent != null)
                _parent.VerifyCheckState();

            this.OnPropertyChanged("IsChecked");
        }

        void VerifyCheckState()
        {
            bool? state = null;
            for (int i = 0; i < this.Children.Count; ++i)
            {
                bool? current = this.Children[i].IsChecked;
                if (i == 0)
                {
                    state = current;
                }
                else if (state != current)
                {
                    state = null;
                    break;
                }
            }
            this.SetIsChecked(state, false, true);
        }

        #endregion // IsChecked

        #endregion // Properties

        #region INotifyPropertyChanged Members

        void OnPropertyChanged(string prop)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion


    }
}